package com.example.a1725185505.works.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a1725185505.works.R;
import com.example.a1725185505.works.base.BaseListener;
import com.example.a1725185505.works.bean.RegisterBean;
import com.example.a1725185505.works.model.RegisterModel;

public class RegisterActivity extends AppCompatActivity {
    private EditText usernameView,passwordView,phoneView,addressView,commentView;
    private Button bt_register;
    private TextView tv_register_fanhui;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        usernameView = (EditText)findViewById(R.id.ed_register_user);
        passwordView = (EditText)findViewById(R.id.ed_register_password);
        phoneView = (EditText)findViewById(R.id.ed_register_phone);
        addressView = (EditText)findViewById(R.id.ed_register_address);
        commentView = (EditText)findViewById(R.id.ed_register_comment);

        tv_register_fanhui = findViewById(R.id.tv_fanhui);
        tv_register_fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent(RegisterActivity.this,MainActivity.class);
                startActivity(intent1);
            }
        });
        bt_register = findViewById(R.id.bt_register);
        bt_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(RegisterActivity.this,MainActivity.class);

                String username = usernameView.getText().toString();
                String passerword = passwordView.getText().toString();
                String phone = phoneView.getText().toString();
                String address = addressView.getText().toString();
                String comment = commentView.getText().toString();

                RegisterModel registerModel = new RegisterModel();
                registerModel.register(username,passerword,phone,address,comment,registerListener);

                startActivity(intent);
                finish();
            }
        });

    }
        BaseListener<RegisterBean> registerListener = new BaseListener<RegisterBean>() {
            @Override
            public void onResponse(RegisterBean registerBean) {
                if (registerBean.getSuccess().equals("0")){
                    Toast.makeText(getApplicationContext(),"注册失败",Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(getApplicationContext(),"注册成功",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_SHORT).show();
            }
        };
}
